//
//  MemberViewController.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/23.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "MemberViewController.h"
#import "ShoppingGoodsContentViewController.h"
#import "MemberTopView.h"
#import "MemberVModel.h"
#import "MemberLastlyWelfareTableViewCell.h"
#import "MemberRightAndInterestsTableViewCell.h"
#import "MemberWelfareChoiceTableViewCell.h"
#import "SupportCategoryGoodListModel.h"

#define Coloreed0af [UIColor colorWithHexString:@"eed0af"]
static NSString *goodsCellID = @"goodsCellID";

static NSString *memberLastWelfareCellID = @"memberLastWelfareCellID";
static NSString *memberRightAndInterestsWelfareCellID = @"memberRightAndInterestsWelfareCellID";
static NSString *memberChoiceWelfareCellID = @"memberChoiceWelfareCellID";

#define topViewHeight jisuan(480)
#define titleScrollHeight jisuan(100)
#define topBottomHeight jisuan(480) + jisuan(100) + jisuan(20)

@interface MemberViewController ()<UIScrollViewDelegate,UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UIScrollView *contentScrollView;

@property (strong, nonatomic) UIView *topContainerView;
@property (strong, nonatomic) MemberTopView *topView;
@property (strong, nonatomic) UIScrollView *titleScrollView;
@property (strong, nonatomic) UITableView *goodsTableView;
@property (strong, nonatomic) UIView *sliderView;

@property (strong, nonatomic) NSMutableArray *labelArr;
@property (strong, nonatomic) NSMutableArray *labelxArr;
@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) UIScrollView *scrollingView;


@property (nonatomic, strong) NSArray *typeOneArray;
@property (nonatomic, strong) NSArray *typeTwoArray;
@property (nonatomic, strong) NSArray *typeThreeArray;
@property (nonatomic, strong) NSArray *listDataArray;

@end

@implementation MemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = Colorf2f2f2;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"趣享淘会员";
 
    [self createTopView];
    [self createUI];
    
    [self.view layoutIfNeeded];
    self.contentScrollView.contentSize = CGSizeMake(WIDTH, MAXFLOAT);
    
}






- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionHeaderView = [[UIView alloc] init];
    sectionHeaderView.backgroundColor = Colorf2f2f2;
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:jisuan(32)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = Color333333;
    label.text = self.array[section];
    [sectionHeaderView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(sectionHeaderView.mas_left);
        make.right.mas_equalTo(sectionHeaderView.mas_right);
        make.top.mas_equalTo(sectionHeaderView.mas_top);
        make.bottom.mas_equalTo(sectionHeaderView.mas_bottom);
        
    }];
    
    return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return jisuan(0.000000001);
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return jisuan(134);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return jisuan(1088);
        
    }else if (indexPath.section == 1) {
        
        return jisuan(390)+jisuan(288)*self.listDataArray.count;
    }else {
        return jisuan(1252); // 1252
    }
    
}

#pragma mark -delegate \ datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    
//    MemberLastlyWelfareTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:memberLastWelfareCellID];
//
//    MemberRightAndInterestsTableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:memberRightAndInterestsWelfareCellID];
//
//    MemberWelfareChoiceTableViewCell *cell3 = [tableView dequeueReusableCellWithIdentifier:memberChoiceWelfareCellID];
//
//    if (!cell1) {
//
//        cell1 = [[MemberLastlyWelfareTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:memberLastWelfareCellID];
//
//    }
//
//    if (!cell2) {
//
//        cell2 = [[MemberRightAndInterestsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:memberRightAndInterestsWelfareCellID];
//    }
//    if (!cell3) {
//
//        cell3 = [[MemberWelfareChoiceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:memberChoiceWelfareCellID];
//    }
//
//    if (indexPath.section == 0) {
//
//        cell = cell1;
//        cell1.parentVC = self;
//        [cell1 reloadDataWithArray:self.typeOneArray];
//
//    }else if (indexPath.section == 1) {
//
//        cell = cell2;
//        cell2.parentVC = self;
//        [cell2 reloadWithDataArray:self.typeTwoArray];
//        [cell2 reloadWithListArray:self.listDataArray];
//    }else {
//
//        cell = cell3;
//        [cell3 reloadCellArray:self.typeThreeArray];
//    }
//
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}





#pragma mark -scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetY = scrollView.contentOffset.y;
    NSIndexPath*path =  [self.goodsTableView indexPathForRowAtPoint:CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y)];
    UILabel *label = self.titleScrollView.subviews[path.section];
    for (int i = 0; i < self.array.count; i ++) {
        UILabel *temp = self.titleScrollView.subviews[i];
        temp.textColor = Color333333;
    }
    
    label.textColor = Coloreed0af;
    CGFloat x = [self.labelxArr[path.section] floatValue];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.1 animations:^{
        CGRect rect = weakSelf.sliderView.frame;
        rect.origin.x = x;
        weakSelf.sliderView.frame = rect;
    }];
    
    
   

    if ([scrollView isKindOfClass:[UITableView class]]) {

    }else {
        if (offsetY > topViewHeight + jisuan(20)) {
            offsetY = topViewHeight + jisuan(20);
            [self.contentScrollView setContentOffset:CGPointMake(0, offsetY) animated:NO];
            CGRect rect = self.goodsTableView.frame;
            rect.size.height = HEIGHT - titleScrollHeight -Height_NavBar ;
            self.goodsTableView.frame = rect;
        }else {
//            CGRect rect = self.goodsTableView.frame;
//            rect.size.height = jisuan(4330);
//            self.goodsTableView.frame = rect;
        }
    }
    self.scrollingView = scrollView;
}



#pragma mark -lable点击
- (void)labelClick:(UIGestureRecognizer *)recognizer {
    
    UILabel *label = (UILabel *)recognizer.view;
    
    for (int i = 0; i < self.array.count; i ++) {
        
        UILabel *temp = self.titleScrollView.subviews[i];
        temp.textColor = Color333333;
    }
    
    label.textColor = Coloreed0af;
    
    CGFloat width = self.titleScrollView.frame.size.width;
    CGFloat offsetX = label.center.x - width * 0.5;
    // 最大偏移量
    CGFloat maxOffsetx = self.titleScrollView.contentSize.width - width;
    
    if (offsetX < 0) {
        
        offsetX = 0;
        
    } else if (offsetX > maxOffsetx) {
        
        offsetX = maxOffsetx;
        
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    CGFloat x = [self.labelxArr[label.tag] floatValue];
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = weakSelf.sliderView.frame;
        rect.origin.x = x;
        weakSelf.sliderView.frame = rect;
    }];
    
    self.contentScrollView.contentOffset = CGPointMake(0, topBottomHeight);

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:label.tag];
    [self.goodsTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
}

- (void)createTitleScrollView {
    
    CGFloat width = WIDTH / 3.0;
    CGFloat x = width / 2.0 - 27;
    
    for (int i = 0; i < self.array.count; i ++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.userInteractionEnabled = YES;
        label.tag = i;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = Font(24);
        label.text = self.array[i];
        [label sizeToFit];
        NSLog(@"%@",NSStringFromCGSize(label.size));
        
        [self.labelArr addObject:label];
        
        if (i == 0) {
            
            label.textColor = Coloreed0af;
            
        }else {
            
            label.textColor = Color333333;
        }
        
        label.frame = CGRectMake(width * i, 0, width, jisuan(100));
        
        CGFloat sliderX = x + width * i;
        [self.labelxArr addObject:[NSString stringWithFormat:@"%.2f", sliderX]];
        
        [self.titleScrollView addSubview:label];
        
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)];
        [label addGestureRecognizer:recognizer];
        
    }
    
    self.sliderView.frame = CGRectMake([self.labelxArr.firstObject floatValue], jisuan(80), 54.3, jisuan(4));
    [self.titleScrollView addSubview:self.sliderView];
    
    self.titleScrollView.contentSize = CGSizeMake(WIDTH, 0);
}

- (void)createTopView {
    
    __weak typeof(self) weakSelf = self;
    [self.view addSubview:self.contentScrollView];
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.right.mas_equalTo(weakSelf.view.mas_right);
        make.top.mas_equalTo(weakSelf.view.mas_top);
//        make.height.mas_equalTo(jisuan(610));
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom);
    }];
    
    
    self.topContainerView.frame = CGRectMake(0, 0, WIDTH, jisuan(730));
    self.topContainerView.frame = CGRectMake(0, 0, WIDTH, topBottomHeight);
    [self.contentScrollView addSubview:self.topContainerView];

    self.topView = [[MemberTopView alloc] init];
    self.topView.parentVC = self;
    [self.topContainerView addSubview:self.topView];

    [self.topContainerView addSubview:self.titleScrollView];

    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(weakSelf.topContainerView.mas_left);
        make.right.mas_equalTo(weakSelf.topContainerView.mas_right);
        make.top.mas_equalTo(weakSelf.topContainerView.mas_top);
//        make.height.mas_equalTo(jisuan(610));
        make.height.mas_equalTo(topViewHeight);
    }];
    [self.titleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(weakSelf.topContainerView.mas_left);
        make.right.mas_equalTo(weakSelf.topContainerView.mas_right);
        make.top.mas_equalTo(weakSelf.topView.mas_bottom).with.offset(jisuan(20));
        make.height.mas_equalTo(titleScrollHeight);
    }];

    [self createTitleScrollView];
    
}

- (void)createUI {
    
    [self.contentScrollView addSubview:self.goodsTableView];
    self.goodsTableView.frame = CGRectMake(0, topBottomHeight, WIDTH, jisuan(5000));
  
}


#pragma mark -lazy
- (UIView *)topContainerView {
    
    if (!_topContainerView) {
        
        _topContainerView = [[UIView alloc] init];
//        _topContainerView.backgroundColor =
    }
    
    return _topContainerView;
}

- (UIScrollView *)contentScrollView {
    
    if (!_contentScrollView) {
        
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.backgroundColor = Colorf2f2f2;
        
        _contentScrollView.delegate = self;
        
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        
        _contentScrollView.canCancelContentTouches =YES;
        
        _contentScrollView.delaysContentTouches = NO;
        
        if (@available(iOS 11.0 , *)) {
            
            _contentScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _contentScrollView;
}
- (UIScrollView *)titleScrollView {
    
    if (!_titleScrollView) {
        
        _titleScrollView = [[UIScrollView alloc] init];
        _titleScrollView.backgroundColor = WhiteColor;
        
        _titleScrollView.delegate = self;
        
        _titleScrollView.showsVerticalScrollIndicator = NO;
        _titleScrollView.showsHorizontalScrollIndicator = NO;
        
    }
    
    return _titleScrollView;
}

- (UITableView *)goodsTableView {
    
    if (!_goodsTableView) {
        
        _goodsTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        _goodsTableView.delegate = self;
        _goodsTableView.dataSource = self;

        _goodsTableView.bounces = NO;
        
        _goodsTableView.estimatedRowHeight = 0;
        _goodsTableView.estimatedSectionFooterHeight = 0;
        _goodsTableView.estimatedSectionHeaderHeight = 0;
        
        _goodsTableView.showsVerticalScrollIndicator = NO;
        _goodsTableView.showsHorizontalScrollIndicator = NO;

        _goodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        if (@available(iOS 11.0, *)) {
            
            _goodsTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _goodsTableView;
    
}

- (NSMutableArray *)labelArr {
    
    if (!_labelArr) {
        
        _labelArr = [NSMutableArray array];
    }
    return _labelArr;
}
- (NSMutableArray *)labelxArr {
    
    if (!_labelxArr) {
        
        _labelxArr = [NSMutableArray array];
    }
    return _labelxArr;
}

- (UIView *)sliderView {
    
    if (!_sliderView) {
        
        _sliderView = [[UIView alloc] init];
        
        _sliderView.backgroundColor = Coloreed0af;
        
    }
    
    return _sliderView;
}

- (NSArray *)array {
    
    if (!_array) {
        
        _array = @[@"最新福利", @"会员权益", @"精选商品"];
    }
    return _array;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
